﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    public class CommentController : ComperioController
    {
        private CommentBLL _commentBLL;

        public CommentBLL CommentBLL
        {
            get
            {
                if (_commentBLL == null)
                    _commentBLL = new CommentBLL();

                return _commentBLL;
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create(String id)
        {
            Session["id"] = URLEncrypt.DecryptFromString(id);      
            ViewData["Comments"] = CommentBLL.RetrieveComments(Convert.ToInt32(Session["id"]));
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "CommentID,SubjectID")] Comment comment)
        {
            ViewData["Comments"] = CommentBLL.RetrieveComments(Convert.ToInt32(Session["id"]));

            if (!ModelState.IsValid)
                return View();

            comment.CreatedDate = DateTime.Now;
            comment.SubjectId = Convert.ToInt32(Session["id"]);

            CommentBLL.Create(comment);
            return View();
        }
    }
}
